package appBCN.version;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import appBCN.MOs.Person;

public class ListHoursActivity extends Activity {
	private Person user;
	private PopupWindow pw;
	private Date date;
	private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
	
	@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.list_times_day);
		Bundle b = getIntent().getExtras();
		try {
			date = dateFormatter.parse(b.getString("date"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		TextView txt=(TextView)findViewById(R.id.txtListView);
		txt.setText(date.toString());
    	user = AppBCNApplication.getInstance().getPerson();
		final String [] items=new String[]{"10:15","10:30","10:45","11:00", "11:15", "11:30", "11:45", "12:00"};
        SimpleAdapterList ad=new SimpleAdapterList(this,android.R.layout.simple_expandable_list_item_1, items);
        final ListView listView=(ListView)findViewById(R.id.listView1);
        listView.setAdapter(ad);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) 
			{
				String textMessage = "Ha escollit "+arg0.getItemAtPosition(arg2)+"\nConfirmar?";
				date.setHours(Integer.parseInt(arg0.getItemAtPosition(arg2).toString().split(":")[0]));
				date.setMinutes(Integer.parseInt(arg0.getItemAtPosition(arg2).toString().split(":")[1]));
				AlertDialog.Builder builder = new AlertDialog.Builder(listView.getContext());
				builder.setMessage(textMessage)
		       .setCancelable(false)
		       .setPositiveButton("Si", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   user.addDate(date);
		        	   AppBCNApplication.getInstance().setPerson(user);
		        	   ListHoursActivity.this.finish();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
				AlertDialog alert = builder.create();
				alert.show();		
			}
		 });
    }
}
